import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('@/pages/index'),
      }
    ]
  },

  {
    path: '/product',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('@/pages/product/index')
      },
      {
        path: 'category',
        component: () => import('@/pages/product/category')
      }
    ]
  },

  {
    path: '/car',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('@/pages/shopCar/index')
      }
    ]
  },

  {
    path: '/order',
    component: Layout,
    children: [
      {
        path: 'obligation',
        component: () => import('@/pages/order/obligation')
      },
      {
        path: 'waitDelivery',
        component: () => import('@/pages/order/waitDelivery')
      },
      {
        path: 'deliveryed',
        component: () => import('@/pages/order/deliveryed')
      },
      {
        path: 'finished',
        component: () => import('@/pages/order/finished')
      },
      {
        path: 'closed',
        component: () => import('@/pages/order/closed')
      },
      {
        path: 'index',
        component: () => import('@/pages/order/index')
      },
    ]
  },

  {
    path: '/address',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('@/pages/address/index')
      }
    ]
  },

  {
    path: '/settlement',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('@/pages/settlement/index')
      }
    ]
  },

  {
    path: '/login',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('@/pages/login')
      }
    ]
  },
  {
    path: '/register',
    component: Layout,
    children: [
      {
        path: '/',
        component: () => import('@/pages/register')
      }
    ]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'history',
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
